package per.zx;

/**
 * @author zhengxin
 * @date 2021/6/30 21:32
 * @description 测试 InheritableThreadLocal
 */
public class TestInheritableThreadLocal {
    // 创建 InheritableThreadLocal
    static ThreadLocal<String> threadLocal = new InheritableThreadLocal<>();

    public static void main(String[] args) throws InterruptedException {
        // 1.主线程设置值
        threadLocal.set("share variable of main");
        // 2.主线程取值打印输出
        System.out.println(Thread.currentThread().getName()+" "+threadLocal.get());
        new Thread(()->{
            // 3.子线程取值打印输出
            System.out.println(Thread.currentThread().getName()+" "+threadLocal.get());
            // 4.字线程设置值
            threadLocal.set("share variable of child");
            // 5.子线程取值打印输出
            System.out.println(Thread.currentThread().getName()+" "+threadLocal.get());
        },"thread-child").start();

        // 6.主线程休眠
        Thread.sleep(1000);

        // 7.主线程取值打印输出
        System.out.println(Thread.currentThread().getName()+" "+threadLocal.get());



    }
}
